//go:build debug

/*
 *  Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 *  This file is part of e3utils.
 *
 *  e3utils is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  e3utils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with e3utils. If not, see <https://www.gnu.org/licenses/>.
 */

//使用 build标签  -i -tags=debug 区分debug 和 release版本

package assert

import (
	"fmt"
	"runtime"
)

// Assert 只能模拟断言,在debug模式下会被求值
func Assert(f func() bool, printParam ...any) {
	if !f() {
		funcName, file, line, _ := runtime.Caller(1)
		s := fmt.Sprintf("===============================\nAssert Fail\n>> File:\n\t%s:%d\n>> Func:%s\n>> Param:%+v\n===============================\n",
			file,
			line,
			runtime.FuncForPC(funcName).Name(),
			printParam,
		)
		panic(s)
	}
}

func AssertNoError(f func() error, printParam ...any) {
	if f() != nil {
		funcName, file, line, _ := runtime.Caller(1)
		s := fmt.Sprintf("===============================\nAssert Fail\n>> File:\n\t%s:%d\n>> Func:%s\n>> Param:%+v\n===============================\n",
			file,
			line,
			runtime.FuncForPC(funcName).Name(),
			printParam,
		)
		panic(s)
	}
}

func EnSure(b bool, printParam ...any) {
	if !b {
		funcName, file, line, _ := runtime.Caller(1)
		s := fmt.Sprintf("===============================\nAssert Fail\n>> File:%s:%d\n>> Func:%s\n>> Param:%+v\n===============================\n",
			file,
			line,
			runtime.FuncForPC(funcName).Name(),
			printParam,
		)
		panic(s)
	}
}

func EnSureNoErr(e error, printParam ...any) {
	if e != nil {
		funcName, file, line, _ := runtime.Caller(1)
		s := fmt.Sprintf("===============================\nAssert Fail\n>> File:%s:%d\n>> Func:%s\n>> Param:%+v\n===============================\n",
			file,
			line,
			runtime.FuncForPC(funcName).Name(),
			printParam,
		)
		panic(s)
	}
}
